package com.efast.dmom.mes.service.impl;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.Workshop;
import com.efast.dmom.mes.datahandlers.entity.WorkShopDataHandler;
import com.efast.dmom.mes.exception.MESException;
import com.efast.dmom.mes.service.WorkShopService;
import com.efast.dmom.mes.util.StringVerifyUtil;

@Service
public class WorkShopServiceImpl implements WorkShopService {
	@Autowired
	private WorkShopDataHandler workShopDataHandler;
	
	@Override
	public ResultBean save(Workshop workShop) throws MESException{
		ResultBean resultBean = new ResultBean(true,"新增成功！");
		if (!StringUtil.isNotBlank(workShop.getInnerData().getName())
				&& !StringUtil.isNotBlank(workShop.getInnerData().getWorkshopCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查车间名称、车间编码是否填写!");
			return resultBean;
			
		}	
		if (!StringVerifyUtil.isstring(workShop.getInnerData().getWorkshopCode())) {
		    resultBean.setSuccess(false);
		    resultBean.setMessage("车间编码不是由英文、字母、下划线组成!!");
		    return resultBean;
		   }
		if (workShop.getInnerData().getPlantId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工厂id不能为空!");
			return resultBean;
		}
		Workshop workshopByName = workShopDataHandler.getByName(workShop.getInnerData().getName());
		Workshop workshopByCode = workShopDataHandler.getByCode(workShop.getInnerData().getWorkshopCode());
		if (workshopByName.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("车间名称不唯一!");
			return resultBean;
		}
		if (workshopByCode.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("车间编码不唯一!");
			return resultBean;
		}
		ResultBean resultBean2 = workShopDataHandler.save(workShop);
		if(!resultBean2.isSuccess()) {
			resultBean.setSuccess(false);
			resultBean.setMessage(resultBean2.getMessage());
			return resultBean;
		}
		resultBean.setObject(resultBean2.getObject());
		return resultBean;
	}

	@Override
	public ResultBean update(Workshop workShop) {
		ResultBean resultBean = new ResultBean(true,"编辑成功！");
		Workshop workshopById = workShopDataHandler.getById(workShop.getInnerData().getId());
		if (workshopById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的车间不存在!");
			return resultBean;
		}
		if (!StringVerifyUtil.isstring(workShop.getInnerData().getWorkshopCode())) {
		    resultBean.setSuccess(false);
		    resultBean.setMessage("车间编码不是由英文、字母、下划线组成!!");
		    return resultBean;
		   }
		if (!workShop.getInnerData().getWorkshopCode().equals(workshopById.getInnerData().getWorkshopCode())
				&& !workShop.getInnerData().getName().equals(workshopById.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("车间编码、车间名称不可修改!");
			return resultBean;
		}
		workShopDataHandler.update(workShop);
		resultBean.setObject(workShop);
		return resultBean;
	}
	
	@Override
	public ResultBean delete(Workshop workShop) {
		ResultBean resultBean = new ResultBean(true,"删除成功！");
		int queryProductionLineById = workShopDataHandler.queryProductionLineById(workShop.getInnerData().getId());
		if (queryProductionLineById >0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("此车间下有产线，不可删除！");
			return resultBean;
		}
		workShopDataHandler.delete(workShop);
		return resultBean;
	}

	@Override
	public Workshop getById(long id) {
		return workShopDataHandler.getById(id);
	}

	@Override
	public Workshop getByName(String name) {
		return workShopDataHandler.getByName(name);
	}

	
    @Override
    public List<Workshop> getWorkshopByPlantId(long plantId) {  
    	return workShopDataHandler.getWorkshopByPlantId(plantId); 
    	}
	 

	@Override
	public Workshop getByCode(String workshopCode) {
		return workShopDataHandler.getByCode(workshopCode);
	}

	@Override
	public List<Map<String, String>> getWorkshopCodeIdNameByPlantId(long plantId) {
		return workShopDataHandler.getWorkshopCodeIdNameByPlantId(plantId);
	}

	@Override
	public ResultBean getWorkshopsByPlantId(long plantId) {
		ResultBean resultBean = new ResultBean(true,"");
		List<Workshop> listWorkshop= workShopDataHandler.getWorkshopByPlantId(plantId);
		resultBean.setObject(listWorkshop);
		return resultBean;
	}

}
